<template>
  <div class="article-comments">
    <!-- 评论列表 -->
    <van-list
      v-model="loading"
      :finished="finished"
      finished-text="没有更多了"
      @load="onLoad"
    >
      <van-cell
        v-for="item in list"
        :key="item.com_id.toString()"
      >
        <van-image
          slot="icon"
          round
          width="30"
          height="30"
          style="margin-right: 10px;"
          :src="item.aut_photo"
        />
        <span
          style="color: #466b9d;"
          slot="title"
        >{{item.aut_name}}</span>
        <div slot="label">
          <p style="color: #363636;">{{item.content}}</p>
          <p>
            <span style="margin-right: 10px;">{{item.pubdate | timeFilter }}</span>
            <van-button
              size="mini"
              type="default"
              @click="hShowReplay(item)"
            >回复</van-button>
          </p>
        </div>
        <van-icon
          slot="right-icon"
          :name="item.is_liking ? 'like':'like-o'"
          @click="hSwitchLikeComment(item)"
        />
      </van-cell>
    </van-list>
    <!-- 评论列表 -->

    <!-- 发布评论 -->
    <van-cell-group class="publish-wrap">
      <van-field
        v-model.trim="content"
        clearable
        placeholder="请输入评论内容"
      >
        <van-button
          @click="hSendCom"
          slot="button"
          size="mini"
          type="info"
        >发布</van-button>
      </van-field>
    </van-cell-group>
    <!-- /发布评论 -->
    <van-popup
      v-model="isReplayShow"
      round
      position="bottom"
      :style="{ height: '85%' }"
    >
      <replay
        v-if="isReplayShow"
        :commentInfo='commentInfo'
        @close-reply="hCloseReply"
      ></replay>
    </van-popup>
  </div>
</template>

<script>
import { getComment, sendComment, likeComment, dislikeComment } from '../../api/comment.js'
import replay from './replay.vue'
export default {
  name: 'ArticleComment',
  data () {
    return {
      list: [], // 评论列表
      loading: false, // 上拉加载更多的 loading
      finished: false, // 是否加载结束
      offset: null,
      content: '',
      isReplayShow: false,
      commentInfo: {}
    }
  },
  components: {
    replay
  },
  props: ['articleId'],
  methods: {
    async onLoad () {
      try {
        const res = await getComment(this.articleId, this.offset)
        // console.log('11', res)
        this.list.push(...res.data.data.results)
        this.loading = false
        this.finished = !res.data.data.results.length
        // 5. 更新offset
        this.offset = res.data.data.last_id
      } catch (err) {
        console.log(err)
        this.loading = false
      }
    },
    async hSendCom () {
      try {
        const res = await sendComment(this.articleId, this.content)
        // console.log('hh', res)
        this.list.push(res.data.data.new_obj)
        this.$toast.success('评论成功')
      } catch (err) {
        console.log(err)
        this.$toast.fail('评论失败')
      }
    },
    hSwitchLikeComment (item) {
      if (item.is_liking) {
        this.hDisLikeCommit(item)
      } else {
        this.hLikeComment(item)
      }
    },
    // 请求点击喜欢评论
    async hLikeComment (item) {
      console.log('item', item)
      try {
        const res = await likeComment(item.com_id.toString())
        console.log(res)
        item.is_liking = true
        this.$toast.success('点赞成功')
      } catch (err) {
        console.log(err)
        this.$toast.fail('点赞失败')
      }
    },
    // 取消评论喜欢
    async hDisLikeCommit (item) {
      try {
        const res = await dislikeComment(item.com_id.toString())
        console.log(res)
        item.is_liking = false
        this.$toast.success('取消点赞成功')
      } catch (err) {
        console.log(err)
        this.$toast.fail('取消点赞失败')
      }
    },
    hShowReplay (item) {
      this.isReplayShow = true
      this.commentInfo = item
      console.log(this.commentInfo)
    },
    hCloseReply () {
      this.isReplayShow = false
    }
  }
}
</script>

<style scoped lang='less'>
// 发表评论的区域是固定在下端的
.publish-wrap {
  position: fixed;
  left: 0;
  bottom: 0;
  width: 100%;
}
// 给发表评论区空出地方
.van-list {
  margin-bottom: 45px;
}
</style>
